﻿Imports System.Data
Imports ADOEntityETL
Imports ADOEntity

Partial Class Form_frmPrintRptApprove
    Inherits System.Web.UI.Page

    'Private ds As New dsDailyETLSync

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
        Dim strStatusPint As String
        Dim strCUSTTYPE As String
        Dim strStartDate As String
        Dim strEndDate As String



        If Not (IsPostBack) Then
            hdfVar1.Value = Request.Item("StatusPint")
            hdfVar2.Value = Request.Item("CUSTTYPE")
            hdfVar3.Value = Request.Item("STARTDATE")
            hdfVar4.Value = Request.Item("ENDDATE")
           
            strStatusPint = Request.Item("StatusPint")
            strCUSTTYPE = Request.Item("CUSTTYPE")
            strStartDate = Request.Item("STARTDATE")
            strEndDate = Request.Item("ENDDATE")

        Else
            If Not (IsCallback) Then
                strStatusPint = convertStringEncode(hdfVar1.Value)
                strCUSTTYPE = convertStringEncode(hdfVar2.Value)
                strStartDate = convertStringEncode(hdfVar3.Value)
                strEndDate = convertStringEncode(hdfVar4.Value)
              
            Else
                strStatusPint = hdfVar1.Value
                strCUSTTYPE = hdfVar2.Value
                strStartDate = hdfVar3.Value
                strEndDate = hdfVar4.Value
             
            End If

        End If
        If (strStatusPint = "1") Then
            Approve(strCUSTTYPE, strStartDate, strEndDate)
        Else
            ApproveNon(strCUSTTYPE, strStartDate, strEndDate)
        End If


    End Sub

    Private Function convertStringEncode(ByVal strCon As String) As String
        Dim rtStr As String = ""
        Try

            rtStr = Encoding.UTF8.GetString(Encoding.GetEncoding("tis-620").GetBytes(strCon))
        Catch ex As Exception
            Throw ex
        End Try
        Return rtStr
    End Function


    Private Sub Approve(ByVal strCusType As String, _
                         ByVal strStartDate As String, ByVal strEndDate As String)

        Dim clsVarParameter As clsVariable
        Dim clsSessionMenuRight As New clsSessionMenuRight
        Dim clsFormatDate As New clsDateFormat
        Dim colVarParameter As Collection = New Collection
        Dim StrWhere As String = ""
        Dim strConcat As String = " where "
        Dim strPrmCondition As String = ""
        Dim strSql As String = ""

        Dim arrCusType As String() = Nothing
        Dim arrBranchCode As String() = Nothing
        Dim arrStatus As String() = Nothing


        Dim ds As New dsKYC
        Dim cls As New clsConnToDbByReport

        Dim re As rptApprove = New rptApprove



        clsSessionMenuRight = Session("ClsMenuRight")


        'If (clsSessionMenuRight.getRoleGroup > 89) Then

        'ElseIf (clsSessionMenuRight.getRoleGroup > 79) Then
        '    StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
        '    strConcat = " And "
        'ElseIf (clsSessionMenuRight.getRoleGroup > 69) Then
        '    StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
        '    strConcat = " And "
        'ElseIf (clsSessionMenuRight.getRoleGroup > 59) Then
        '    StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "' and ur.ZONECODE='" & clsSessionMenuRight.getZoneCode & "'", StrWhere, strConcat)
        '    strConcat = " And "

        'ElseIf (clsSessionMenuRight.getRoleGroup > 49) Then
        '    StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
        '    strConcat = " And "

        'ElseIf (clsSessionMenuRight.getRoleGroup < 50) Then
        '    StrWhere = String.Format("{0}{1}ur.USERNAME = '" & clsSessionMenuRight.getUserName & "'", StrWhere, strConcat)
        '    strConcat = " And "
        'End If

        If Not clsSessionMenuRight.getUserName = "" Then
            StrWhere = String.Format("{0}{1}l.APPROVENAME = '" & clsSessionMenuRight.getUserName & "'", StrWhere, strConcat)
            strConcat = " And "

        End If


        If Not strCusType = "" Then
            arrCusType = strCusType.Split("/")
            If Not arrCusType(0) = "0" Then
                StrWhere = String.Format("{0}{1}c.CUSTTYPE = :CUSTTYPE", StrWhere, strConcat)
                strConcat = " And "

                clsVarParameter = New clsVariable() With {.nameColumn = "CUSTTYPE", _
                                                          .valueColumn = arrCusType(0)}
                colVarParameter.Add(clsVarParameter)


                re.prmCustype.Value = arrCusType(1)

            Else
                re.prmCustype.Value = " - "

            End If
        End If

        If Not (strStartDate = "" And strEndDate = "") Then
            Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strStartDate) & " 00:00:00"
            Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strEndDate) & " 23:59:59"
            StrWhere = String.Format("{0}{1}l.APPROVEDATE >= :StartDate and l.APPROVEDATE <= :EndDate", _
                                     StrWhere, _
                                     strConcat)
            strConcat = " And "
            clsVarParameter = New clsVariable() With {.nameColumn = "StartDate", _
                                                      .valueColumn = dteStartDate}
            colVarParameter.Add(clsVarParameter)

            clsVarParameter = New clsVariable() With {.nameColumn = "EndDate", _
                                                      .valueColumn = dteEndDate}
            colVarParameter.Add(clsVarParameter)

            re.prmStartDate.Value = dteStartDate
            re.prmEndDate.Value = dteEndDate
        Else
            re.prmStartDate.Value = " - "
            re.prmEndDate.Value = " - "

        End If
 
        strSql = "select l.*,c.THFIRSTNAME,c.THMIDNAME,c.THLASTNAME," _
                    & " c.ENFIRSTNAME, c.ENMIDNAME, c.ENLASTNAME," _
                    & " c.CUSTTYPE, c.UNIQUEID, c.CUSTCODE," _
                    & " c.AMLCDD, c.AMLREASON, c.AMLCDDOVERVIEW, c.AMLREASONOVERVIEW, " _
                    & " s.APPROVENAME APPROVESTATUS" _
                    & " from " & System.Configuration.ConfigurationSettings.AppSettings("TableLog_Approve") & " l " _
                    & " left join " & System.Configuration.ConfigurationSettings.AppSettings("TableMCUSTOMER") & " c on l.CUSTSEQ = c.CUSTSEQ " _
                    & " left join " & System.Configuration.ConfigurationSettings.AppSettings("TableMAPPROVESTATUS") & " s on l.ApproveCode=s.ApproveCode " _
                    & StrWhere _
                    & " order by l.APPROVEDATE desc "


        cls.rptApprove(re, strSql, ds, colVarParameter)
        ' re.prmWhere.Value = strPrmCondition
        ReportViewer1.Report = re

    End Sub
    Private Sub ApproveNon(ByVal strCusType As String, _
                       ByVal strStartDate As String, ByVal strEndDate As String)

        Dim clsVarParameter As clsVariable
        Dim clsSessionMenuRight As New clsSessionMenuRight
        Dim clsFormatDate As New clsDateFormat
        Dim colVarParameter As Collection = New Collection
        Dim StrWhere As String = ""
        Dim strConcat As String = " where "
        Dim strPrmCondition As String = ""
        Dim strSql As String = ""

        Dim arrCusType As String() = Nothing
        Dim arrBranchCode As String() = Nothing
        Dim arrStatus As String() = Nothing


        Dim ds As New dsKYC
        Dim cls As New clsConnToDbByReport

        Dim re As rptApproveNon = New rptApproveNon



        clsSessionMenuRight = Session("ClsMenuRight")


        'If (clsSessionMenuRight.getRoleGroup > 89) Then

        'ElseIf (clsSessionMenuRight.getRoleGroup > 79) Then
        '    StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
        '    strConcat = " And "
        'ElseIf (clsSessionMenuRight.getRoleGroup > 69) Then
        '    StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
        '    strConcat = " And "
        'ElseIf (clsSessionMenuRight.getRoleGroup > 59) Then
        '    StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "' and ur.ZONECODE='" & clsSessionMenuRight.getZoneCode & "'", StrWhere, strConcat)
        '    strConcat = " And "

        'ElseIf (clsSessionMenuRight.getRoleGroup > 49) Then
        '    StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
        '    strConcat = " And "

        'ElseIf (clsSessionMenuRight.getRoleGroup < 50) Then
        '    StrWhere = String.Format("{0}{1}ur.USERNAME = '" & clsSessionMenuRight.getUserName & "'", StrWhere, strConcat)
        '    strConcat = " And "
        'End If

        If Not clsSessionMenuRight.getUserName = "" Then
            StrWhere = String.Format("{0}{1}l.APPROVENAME = '" & clsSessionMenuRight.getUserName & "'", StrWhere, strConcat)
            strConcat = " And "

        End If


        If Not strCusType = "" Then
            arrCusType = strCusType.Split("/")
            If Not arrCusType(0) = "0" Then
                StrWhere = String.Format("{0}{1}c.CUSTTYPE = :CUSTTYPE", StrWhere, strConcat)
                strConcat = " And "

                clsVarParameter = New clsVariable() With {.nameColumn = "CUSTTYPE", _
                                                          .valueColumn = arrCusType(0)}
                colVarParameter.Add(clsVarParameter)


                re.prmCustype.Value = arrCusType(1)

            Else
                re.prmCustype.Value = " - "

            End If
        End If

        If Not (strStartDate = "" And strEndDate = "") Then
            Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strStartDate) & " 00:00:00"
            Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strEndDate) & " 23:59:59"
            StrWhere = String.Format("{0}{1}l.APPROVEDATE >= :StartDate and l.APPROVEDATE <= :EndDate", _
                                     StrWhere, _
                                     strConcat)
            strConcat = " And "
            clsVarParameter = New clsVariable() With {.nameColumn = "StartDate", _
                                                      .valueColumn = dteStartDate}
            colVarParameter.Add(clsVarParameter)

            clsVarParameter = New clsVariable() With {.nameColumn = "EndDate", _
                                                      .valueColumn = dteEndDate}
            colVarParameter.Add(clsVarParameter)

            re.prmStartDate.Value = dteStartDate
            re.prmEndDate.Value = dteEndDate
        Else
            re.prmStartDate.Value = " - "
            re.prmEndDate.Value = " - "

        End If



   
        strSql = "select l.*,c.THFIRSTNAME,c.THMIDNAME,c.THLASTNAME," _
                    & " c.ENFIRSTNAME, c.ENMIDNAME, c.ENLASTNAME," _
                    & " c.CUSTTYPE, c.UNIQUEID, c.CUSTCODE," _
                    & " c.AMLCDD, c.AMLREASON, c.AMLCDDOVERVIEW, c.AMLREASONOVERVIEW, " _
                    & " s.APPROVENAME APPROVESTATUS" _
                    & " from " & System.Configuration.ConfigurationSettings.AppSettings("TableLog_Approve") & " l " _
                    & " left join " & System.Configuration.ConfigurationSettings.AppSettings("TableMCUSTOMER") & " c on l.CUSTSEQ = c.CUSTSEQ " _
                    & " left join " & System.Configuration.ConfigurationSettings.AppSettings("TableMAPPROVESTATUS") & " s on l.ApproveCode=s.ApproveCode " _
                    & StrWhere _
                    & " order by l.APPROVEDATE desc "


        cls.rptApprove(re, strSql, ds, colVarParameter)
        ' re.prmWhere.Value = strPrmCondition
        ReportViewer1.Report = re

    End Sub
  
End Class
